""" VOTable Examples
	Created 2005-06-01 by Shui Hung Kwok, shkwok at computer.org
	This is a collection of examples to show how to use the VOTable class.
"""

Usage="""
VOTable Examples:

Usage: python VOTableEx.py exampleName parameters ...
For example: python VOTableEx.py printRaw data/file.xml

exampleName can be:
printRaw source
	source can be URL or file name
	Reads and outputs a VOTable.

printVOTable source	fname
	source can be URL or file name
	fname is output file name
	Reads and outputs a VOTable to a file.

printFields source
	source can be URL or file name
	Reads a VOTable and outputs list of fields.

printCSV source
	source can be URL or file name
	Reads a VOTable and outputs content in CSV format.

printNode source path
	source can be URL or file name
	Reads a VOTable and outputs the content of a given node.

runAll
	runs all examples
"""

import sys
from  VOTable import VOTable

def printRaw (source):
	""" Reads a VOTable and outputs it on standard output
		argv is a list of containing one name: source of VOTable.
	"""
	votable = VOTable (source)
	votable.printAllNodes ()


def printVOTable (source, fname):
	""" Reads a VOTable and outputs to a file.
		argv = [Source of VOTable, Output file name]
	"""
	def writer (str):
		fh.write (str)

	fh = file (fname, "w")
	votable = VOTable (source)
	votable.printAllNodes (writer)
	print "Output written to %s" % fname
	fh.close ()

def printFields (source):
	"""	Reads a VOTable and outputs the name of the fields
	"""
	votable = VOTable (source)
	fields = votable.getFields ()
	for i, fd in enumerate (fields):
		attr = fd.getAttributes ()
		try: description = fd.DESCRIPTION.content
		except: description = ''
		name = attr.get ('name')
		ucd = attr.get ('ucd')
		print "Field[%d]= %s,%s,%s" % (i, name, ucd, description)
	
def printCSV (source):
	""" Reads a VOTAble.
		Outputs the content of the VOTable in comma separated values
		with fields names in the first line.
		Output can be read with any Spreadsheet program.
	"""
	votable = VOTable (source)
	fields = votable.getFieldsAttrs ()
	header = [fd['name'] for fd in fields]
	print ','.join (header)

	rows = votable.getDataRows ()
	getData = votable.getData
	for row in rows:
		print ','.join (getData(row))
	
def printNode (source, path):
	""" Reads a VOTable and outputs the content of a given node.
		The node is addressed with 'tag1/tag2/tag3'.
		Array indices can be used also, like 'tag1/tag2[3]/tag4'.
	"""
	votable = VOTable (source)
	try:
		print votable.getContent (path)
	except:
		print "Sorry %s not found" % path
	
def runAll ():
	""" Tests all examples
	"""
	print "\nPrint Raw:\n%s" % ('*' * 40)
	printRaw ('../data/l.xml')
	print "\nPrint VOTable:\n%s" % ('*' * 40)
	printVOTable ('../data/l.xml', 'out')
	print "\nPrint fields:\n%s" % ('*' * 40)
	printFields ('../data/l.xml')
	print "\nPrint CSV:\n%s" % ('*' * 40)
	printCSV ('../data/l.xml')
	print "\nPrint node:\n%s" % ('*' * 40)
	printNode ('../data/l.xml', '/VOTABLE/DESCRIPTION')
	pass

if __name__ == "__main__":
	try:
		vars ()[sys.argv[1]](*sys.argv[2:])
	except Exception, e:
		print "Failed to execute example ", e
		print Usage

